import matplotlib.pyplot as plt
import pandas as pd
import os
import sys
import numpy as np
import matplotlib.pyplot as plt

def Monotonic(path):
    df = pd.read_csv(path)

    x = list(df['x'])

    state_change_count = 0
    state_list = []
    state = x[0] > x[1] # True表示递减;False表示递增
    state_list.append(state)
    for i in range(2, len(x)):
        tmp_state = x[i-1] > x[i]
        if tmp_state != state:
            state_change_count += 1
            state_list.append(tmp_state)
            # print("{}: {}->{}".format(i, state, tmp_state))
        state = tmp_state

    return state_change_count, state_list

def transform(path):
    df = pd.read_csv(path)
    x = list(df['x'])

    # x = [0, 0.2, 0.4, 0.6, 0.8, 1.0, 0.21, 0.41, 0.61, 0.81, 1.0]

    stack = [df.loc[0].copy()]
    i = 1
    while i < len(x) and x[i-1] < x[i]:
        stack.append(df.loc[i].copy())
        i = i+1

    n = i
    for i in range(n):
        df.loc[i] = stack[n-i-1]

    print(len(stack))
    # print(stack)

    arr = np.asarray(df)
    print(arr)
    plt.plot(arr[:, 0], arr[:, 1])
    plt.ylim(ymin=-0.3, ymax=0.3)
    plt.show()

    sys.exit()
    pass

if __name__ == "__main__":
    dir = "D:/data_cmp/airfoil_csv"

    transform("D:/data_cmp/airfoil_csv/S102B.csv")

    # for file in os.listdir(dir):
    #     path = os.path.join(dir, file)
    #     count, state = Monotonic(path)
    #     if count != 1 and not (state[0] is True and state[1] is False):
    #         print("{}: {}, {}".format(file, count, state))
            # transform(path)
